package 算法.leetcode.labuladong.数组和链表;

/**
 * @author lchenglong
 * @date 2022/6/20
 */
public class 接雨水 {
    class Solution {
        public int trap(int[] height) {
            int left = 0,right = height.length-1;
            int l_max = height[left],r_max = height[right];
            int res = 0;
            while (left<right){
                l_max = Math.max(l_max,height[left]);
                r_max = Math.max(r_max,height[right]);
                // res += Math.max(l_max,r_max)-height[i]
                if (l_max<r_max){
                    // 此时水一定能接住,因为r_max > l_max
                    res += l_max-height[left];
                    left++;
                } else {
                    res += r_max - height[right];
                    right--;
                }
            }
            return res;
        }
    }
}
